<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2025/7/21
 * Time: 11:35
 */

namespace app\service;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class BaiduKeywordsSpotting {
    private $client;
    private $proxies = [
        'http://121.40.110.232:8080', // 替换为你的代理IP
        'http://121.40.110.232:8081'
    ];
    private $userAgents = [
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
    ];

    public function __construct() {
        $this->client = new Client([
            'timeout' => 10,
            'headers' => $this->getRandomHeaders(),
        ]);
    }

    // 生成随机HTTP头
    private function getRandomHeaders() {
        return [
            'User-Agent' => $this->userAgents[array_rand($this->userAgents)],
            // 'Accept' => '*/*',
            'Accept-Language' => 'zh-CN,zh;q=0.9,pt;q=0.8',
            'Connection' => 'keep-alive',
            'Cookie' => 'BIDUPSID=152EBEB0BFC5ADC0611FCF78C8672066; PSTM=1725609796; BD_UPN=12314753; MAWEBCUID=web_eZLBNnCJBfenAEebAKRZWheDeEmblkfFomuYdolxPZfzZFIfwK; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; MSA_WH=1522_735; BAIDUID=5025D37BD7CF7FFF6B707350425B8E0B:SL=0:NR=10:FG=1; BDUSS=IyMkgxZ1B3WnVIb1pjOHRhZktXeVRqTWItUk80V0I1R0tTMjZHRkdKbzlQYVZvSVFBQUFBJCQAAAAAAAAAAAEAAADyRVI3ufq3~rXa0ru12MbMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2wfWg9sH1oLU; BDUSS_BFESS=IyMkgxZ1B3WnVIb1pjOHRhZktXeVRqTWItUk80V0I1R0tTMjZHRkdKbzlQYVZvSVFBQUFBJCQAAAAAAAAAAAEAAADyRVI3ufq3~rXa0ru12MbMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2wfWg9sH1oLU; BAIDUID_BFESS=5025D37BD7CF7FFF6B707350425B8E0B:SL=0:NR=10:FG=1; delPer=0; BD_CK_SAM=1; PSINO=5; BA_HECTOR=0kaka585808l0ka40ga5850g0ka18n1k7rc2424; ZFY=YgYwWSttnEN6qg56eqPWU:BZOtkV0squIdRlBj6AbFiI:C; H_WISE_SIDS=62325_63140_63326_63275_63808_63881_63992_64010_64014_64026_64057_64048_64056_64085_64145; sug=3; sugstore=0; ORIGIN=2; bdime=0; baikeVisitId=bda94dc0-74b6-4143-9bd6-6602ef0f6ad3; Hm_lvt_aec699bb6442ba076c8981c6dc490771=1752741679,1753066340,1753066722,1753079158; Hm_lpvt_aec699bb6442ba076c8981c6dc490771=1753079158; HMACCOUNT=661C09D80EDD87DF; H_PS_PSSID=62325_63140_63326_63275_63808_63881_63992_64010_64014_64026_64057_64048_64056_64085_64145; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; COOKIE_SESSION=238_0_2_9_1_7_1_0_1_5_0_1_0_0_3_0_1753079356_0_1753079353%7C9%2325_134_1750748545%7C9; RT="z=1&dm=baidu.com&si=beb9df81-4c4a-4c1d-89d0-4e3a3951292b&ss=mdcexiyd&sl=2z&tt=1unu&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=blf55"; H_PS_645EC=9d28s9JZLB3QcwGeBaV1B4vBGh5ao1tLdos5nhxmI%2FAAF%2Fa1zWe3CVe754KYTDMOXEp0; BDSVRTM=1; ZD_ENTRY=baidu; ab_sr=1.0.1_ZTlmYTNkZDJjZDNjYmJjMmU2ZjVhM2RmODVmMTEzMGQ4ODhhNDkwOTgzNTFjYjBjZTAzNTNlNmNjMDI3ZTY5NDQ5MGNmOTU2Y2M4MDQ0ZmM3NDUwNDRhN2RjYmY2ODU5ZDBmYjJhMzNiMTk4NjQ3OGNhMWNlNDM1NWExYjc1YmVjMmRhZTdiYjlkY2JiMGJmMTUxMzE0YmZhZmI3NmUxZWVjNmYwZTYxMmRjMTZjMGRmMzI3YjIzN2E0NTE5ZmZl; WWW_ST=1753079957257'
        ];
    }

    // 获取随机代理IP
    private function getRandomProxy() {
        return ['proxy' => $this->proxies[array_rand($this->proxies)]];
    }

    // 抓取百度搜索结果
    public function search($keyword, $page = 1) {
        $url = "https://www.baidu.com/s?wd=" . urlencode($keyword) . "&pn=" . (($page - 1) * 10);
        try {
            // 随机延迟1-3秒
            sleep(mt_rand(1, 3));
            $response = $this->client->get($url, [
                'proxy' => $this->getRandomProxy(), // 可选：使用代理
                // 'proxy' => 'http://user:pass@high-anonymous-proxy.com:8080', // 高匿代理
                'headers' => $this->getRandomHeaders(),
            ]);
            $html = (string)$response->getBody();
            $results = $this->parseResults($html,$keyword);
            return $results;
        } catch (RequestException $e) {
            return [
                'status' => 'error',
                'message' => $e->getMessage(),
            ];
        }
    }

    // 解析搜索结果
    private function parseResults($html,$keyword) {
        // dd($html);
        $results = [];
        $pattern = '/<div class="z2wtsd7 f13 c-gap-top-xsmall">(.*?)<\/div>/s';

        @preg_match_all($pattern, $html, $matches);
        foreach ($matches[1] AS $key => $match) {
            // dd($match);
            preg_match('/"title":"([^"]*)"/', $match, $cs);
            preg_match('/"url":"([^"]*)"/', $match, $csc);
            if (strpos($match,'温州一飞信息传媒有限公司'))
                $results = [
                    '关键词' => $keyword,
                    '位置' => $key,
                    '企业' => $cs[1],
                    '链接' => $csc[1]
                ];
        }
        return $results ?? [];
    }
}
